<?php

namespace App\Models;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 门店信息模型
 *
 * Class Shop
 * @property $users
 * @property $tasks
 * @property $company_id
 * @package App\Models
 */
class Shop extends Model
{
    use SoftDeletes;

	/**
	 * 定义可批量填充字段
	 *
	 * @var array
	 * Date: 2018/2/5
	 * @author George
	 */
    protected $fillable = [
        'company_id',
        'name',
        'code',
        'address',
        'contacts',
        'mobile',
        'telephone'
    ];

    /**
     * 自定义隐式路由绑定实现缓存
     *
     * Date: 2018/1/25
     * @author George
     * @param mixed $value
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function resolveRouteBinding($value)
    {
        return Cache::remember("shop:{$value}", 60, function () use ($value) {
            return $this->where($this->getRouteKeyName(), $value)->first();
        });
    }

    /**
     * 获取门店的用户
     *
     * Data: 2018/2/1
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users(){
        return $this->belongsToMany(User::class, 'shop_user_pivot', 'shop_id', 'user_id');
    }

    /**
     * 获取门店的任务
     *
     * Data: 2018/2/1
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function tasks(){
        return $this->belongsToMany(Task::class, 'shop_task_pivot', 'shop_id', 'task_id');
    }

    /**
     * 获取门店的任务提交数据
     *
     * @author Eric
     */
    public function xunDianJiLus()
    {
        return $this->hasMany(XunDianJiLu::class,'shop_id','id');
    }
}
